package net.bright.demo.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bright.demo.ext.beetl.MyBeetlRender;

import com.jfinal.handler.Handler;
import com.jfinal.log.Logger;

/**
 * 全局Handler，设置一些通用功能
 */
public class BloglHandler extends Handler {
	private static Logger log = Logger.getLogger(BloglHandler.class);

	@Override
	public void handle(String target, HttpServletRequest request,
			HttpServletResponse response, boolean[] isHandled) {
		if (target.startsWith("/blog")) {
			log.warn("请求处理开始");
			long starttime = System.currentTimeMillis();

			nextHandler.handle(target, request, response, isHandled);

			log.warn("请求处理完毕，计算耗时");
			// 结束时间
			long endtime = System.currentTimeMillis();
			// 总耗时
			Long totalTime = endtime - starttime;
			// 视图耗时
			Long renderTime = 0L;
			if (null != request.getAttribute(MyBeetlRender.renderTimeKey)) {
				renderTime = (Long) request
						.getAttribute(MyBeetlRender.renderTimeKey);
			}
			// action耗时
			Long actionTime = totalTime - renderTime;

			log.warn("总耗时：" + totalTime + ", 视图耗时：" + renderTime + ", Action耗时："
					+ actionTime);
		} else {
			nextHandler.handle(target, request, response, isHandled);
		}
		

	}
}
